package com.edunera;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.edunera.models.edu_comments;

import java.util.ArrayList;


public class FragComment extends Fragment {
    private ArrayList<edu_comments> comments;

    LayoutInflater inflater;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_comment, null);

        this.inflater = getLayoutInflater(savedInstanceState);
        comments = (ArrayList<edu_comments>) getArguments().getSerializable("comments");
        ListView listView = (ListView) view.findViewById(R.id.listView);
        TextView emptyView = (TextView) view.findViewById(R.id.emptyView);
        listView.setEmptyView(emptyView);
        listView.setAdapter(new MyAdapter());
        return view;

    }


    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return comments.size();

        }

        @Override
        public edu_comments getItem(int position) {
            return comments.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;

        }

        private class ViewHolder {
            TextView comment;
            TextView subject;
            TextView username;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            ViewHolder holder;
            if(convertView ==null) {
                convertView = inflater.inflate(R.layout.list_item_comment,viewGroup,false);
                holder = new ViewHolder();
                holder.comment = (TextView) convertView.findViewById(R.id.comment);
                holder.username = (TextView) convertView.findViewById(R.id.username);
                holder.subject = (TextView) convertView.findViewById(R.id.subject);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            edu_comments comment = getItem(position);
            holder.username.setText(comment.username);
            holder.subject.setText(comment.subject);
            holder.comment.setText(comment.comment);

            return convertView;

        }
    }
}
